package com.demodis.action;

import com.demodis.domain.Json;
import com.demodis.domain.TblDeterGroup;
import com.demodis.domain.TblUser;
import com.demodis.service.DeterGroupServiceI;
import com.demodis.service.PermissionServiceI;
import com.demodis.service.impl.DeterGroupServiceImpl;
import com.demodis.service.impl.PermissionServiceImpl;
import com.opensymphony.xwork2.ModelDriven;

public class DeterGroupAction extends BaseAction implements ModelDriven<TblDeterGroup> {


	private static final long serialVersionUID = 2619060334307584567L;

	DeterGroupServiceI deterService = new DeterGroupServiceImpl();
	PermissionServiceI permService = new PermissionServiceImpl();
	TblDeterGroup deterGroup = new TblDeterGroup();
	
	@Override
	public String execute() throws Exception {
		String GDRURL = "/deterGroupAction!getDeterResult.action";
		String GDRCUURL = "/deterGroupAction!getDeterResultCU.action";
		String DETGRUPURL = "/deterGroupAction!getDetManaGroups.action";
		TblUser user = super.getCurrentUser();
		if(permService.existPerm(user.getUserId(), GDRURL)) {
			user.setDeterUrl(GDRURL);
		} else if(permService.existPerm(user.getUserId(), GDRCUURL)) {
			user.setDeterUrl(GDRCUURL);
		}else if(permService.existPerm(user.getUserId(), DETGRUPURL)) {
			user.setDeterUrl(DETGRUPURL);
		}
		
		String URL = "/userAction!queryUserStat.action";
		String CUURL = "/userAction!queryCUG.action";
		if(permService.existPerm(user.getUserId(), URL)) {
			user.setQueryStatUrl(URL);
		} else if(permService.existPerm(user.getUserId(), CUURL)) {
			user.setQueryStatUrl(CUURL);
		}
		
		return "success";
	}
	
	public void query() {
		super.writeJson(deterService.getDeterGroupsRCU(super.getCurrentUser()));
	}
	
	public void getDeterResultCU() {
		super.writeJson(deterService.getDeterGroupsCU(super.getCurrentUser()));
	}
	public void getDeterResult() {
		super.writeJson(deterService.getDeterGroups());
	}

	//add by robbie
	public void getDetManaGroups() {
		super.writeJson(deterService.getManageGroups(super.getCurrentUser()));
	}
	
	
	public void add() {
		Json json = new Json();
		if (deterService.save(deterGroup)) {
			json.setSuccess(true);
			json.setMsg("添加成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("添加失败!");
		}
		super.writeJson(json);
	}

	public void modify() {
		Json json = new Json();
		if (deterService.modify(deterGroup)) {
			json.setSuccess(true);
			json.setMsg("修改成功!");
			json.setObject(deterGroup);
		} else {
			json.setSuccess(false);
			json.setMsg("修改失败!");
		}

		super.writeJson(json);
	}

	public void delete() {
		Json json = new Json();
		if (deterService.delete(deterGroup)) {
			json.setSuccess(true);
			json.setMsg("删除成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("删除失败!");
		}

		super.writeJson(json);
	}
	
	public void doDeter() {
		TblUser user = super.getCurrentUser();
		Json json = new Json();
		if(user.getDetered() == 2) {
			json.setSuccess(false);
			json.setMsg("你已测评过了！");
		} else if (deterService.doDeter(deterGroup, user)) {
			user.setDetered(2);
			super.addAttributeForSession("user", user);
			json.setSuccess(true);
			json.setMsg("测评成功!");
		} else {
			json.setSuccess(false);
			json.setMsg("测评失败!");
		}
		
		super.writeJson(json);
	}
	
	@Override
	public TblDeterGroup getModel() {
		return deterGroup;
	}

}
